package pack.model;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;

import pack.model.CONSTANTS.LevelSize;
/** 
 * This class is used to generate a list of world objects from a text file.
 * @author Oscar Nyblom
 */
public class LevelParser {
	
	//----------- Class Variables ------------
	/**
	 * Singleton Instance
	 */
	private static LevelParser me;
	
	//----------- Functions -----------------
	
	/**
	 * Singleton function
	 * 
	 * @return
	 */
	public static LevelParser getInstance(){
		if(me == null)
			me = new LevelParser();
		return me;
	}

	/**
	 * This function parses a text document into
	 * a map that can be used in the game.
	 * 
	 * Text document syntax:
	 * 
	 * P = Player
	 * E = Enemy
	 * W = Wall
	 * H = Hole
	 * K = Key
	 * D = Door
	 * * = Diamond
	 * 
	 * @param fileName
	 * @return
	 */
	public ArrayList<WorldObject> parseLevel(String fileName, LevelSize levelSize){
		
		System.out.println("Creating map from file: " + fileName + "...");
		FileHandle handle = Gdx.files.internal(fileName);
		String mapText = handle.readString();
		ArrayList<WorldObject> levelMap = new ArrayList<WorldObject>();
		int size = 15;
		int mapIndex = 0;
		
		System.out.println("Determening size...");
		
		switch (levelSize){
			case SMALL: size = 15; break;
			case MEDIUM: size = 25; break;
			case BIG: size = 35; break;
		}
		
		System.out.println("Adding objects...");
		for(int vIndex = size-1; vIndex >= 0; vIndex--){
			for(int hIndex = 0; hIndex < size; hIndex++){
				
				if(mapText.charAt(mapIndex) == 'P')
					levelMap.add(new Player(CONSTANTS.squareSize*hIndex, CONSTANTS.squareSize*vIndex));
				else if(mapText.charAt(mapIndex) == 'E')
					levelMap.add(new Enemy(CONSTANTS.squareSize*hIndex, CONSTANTS.squareSize*vIndex));
				else if(mapText.charAt(mapIndex) == 'W')
					levelMap.add(new Wall(CONSTANTS.squareSize*hIndex, CONSTANTS.squareSize*vIndex));
				else if(mapText.charAt(mapIndex) == 'H')
					levelMap.add(new Hole(CONSTANTS.squareSize*hIndex, CONSTANTS.squareSize*vIndex));
				else if(mapText.charAt(mapIndex) == 'K')
					levelMap.add(new Key(CONSTANTS.squareSize*hIndex, CONSTANTS.squareSize*vIndex));
				else if(mapText.charAt(mapIndex) == 'D')
					levelMap.add(new Door(CONSTANTS.squareSize*hIndex, CONSTANTS.squareSize*vIndex));
				else if(mapText.charAt(mapIndex) == '*')
					levelMap.add(new Diamond(CONSTANTS.squareSize*hIndex, CONSTANTS.squareSize*vIndex));
				else if(mapText.charAt(mapIndex) == 'L')
					levelMap.add(new Lever(CONSTANTS.squareSize*hIndex, CONSTANTS.squareSize*vIndex));
				else if(mapText.charAt(mapIndex) == 'G')
					levelMap.add(new Goal(CONSTANTS.squareSize*hIndex, CONSTANTS.squareSize*vIndex));
				else if(mapText.charAt(mapIndex) == '\n')
					hIndex= hIndex-2;
				mapIndex++;
			}
		}
		
		return levelMap;
	}
	
	public String readFile(String fileName){
		String text = "";
		
		try {
		    BufferedReader in = new BufferedReader(new FileReader(fileName));
		    String str;
		    while ((str = in.readLine()) != null) {
		        text = text + str + "\n";
		    }
		    in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return text;
	}
}
